//
//  IrcleStatz.m
//  Statz
//
//  Created by dmaclach on 12/04/07.
//
// Copyright 2007 Nightfall Software.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
// the License.
//

#import "IrcleStatz.h"
#import "AEVTBuilder.h"
#import "StatusUpdate.h"

@implementation IrcleStatz

- (NSString*)appBundleID {
  return @"com.ircle.ircle";
}

- (NSString*)uuid {
  return @"18809D24-ED3C-4B06-AB66-A52355EA8505";
}

- (void)dealloc {
  [oldValue_ release];
  [super dealloc];
}

- (StatzUpdateValue)updateStatz:(StatusUpdate*)sender {
  NSString *newValue = [sender status];
  StatzState newState = [sender state];
  if (newState != kStatzStateAway) {
    newValue = @"";
  }
  if ([newValue isEqualToString:oldValue_]) return kStatzUpdateNotChanged;
  NSAppleEventDescriptor *getConnectionCount = [AEVT  class:kAECoreSuite 
                                                         id:kAECountElements
                                             targetBundleID:[self appBundleID],
                                                [KEY:keyDirectObject], [DESC null],
                                                [KEY:keyAEObjectClass], [TYPE : 'CONN'],
                                                ENDRECORD]; 
  require_ae_noerr(getConnectionCount, cantHandleAppleEvent);
  NSAppleEventDescriptor *reply = [getConnectionCount sendWithImmediateReply];
  require_ae_noerr(reply, cantHandleAppleEvent);
  NSAppleEventDescriptor *connectionCountDesc = [reply paramDescriptorForKeyword:keyDirectObject];
  require_ae_noerr(connectionCountDesc, cantHandleAppleEvent);
  int count = [connectionCountDesc int32Value];
  for (int i = 1; i <= count; i++) {
    NSAppleEventDescriptor *isConnected = [AEVT class:kAECoreSuite
                                                   id:kAEGetData
                                       targetBundleID:[self appBundleID],
                                           [KEY : keyDirectObject],
                                           [RECORD : cObjectSpecifier,
                                            [KEY : keyAEKeyForm], [ENUM : formPropertyID],
                                            [KEY : keyAEDesiredClass], [TYPE : cProperty],
                                            [KEY : keyAEKeyData], [TYPE : 'STAT'],
                                            [KEY : keyAEContainer], 
                                            [RECORD : cObjectSpecifier,
                                             [KEY : keyAEKeyForm], [ENUM : formAbsolutePosition],
                                             [KEY : keyAEDesiredClass], [TYPE : 'CONN'],
                                             [KEY : keyAEKeyData], [INT : i],
                                             [KEY:keyAEContainer], [DESC null],
                                             ENDRECORD],
                                            ENDRECORD],
                                           ENDRECORD];
    require_ae_noerr(isConnected, cantHandleAppleEvent);
    reply = [isConnected sendWithImmediateReply];
    require_ae_noerr(reply, cantHandleAppleEvent);
    NSAppleEventDescriptor *connectedValueDesc = [reply paramDescriptorForKeyword:keyDirectObject];
    require_ae_noerr(connectedValueDesc, cantHandleAppleEvent);
    int state = [connectedValueDesc enumCodeValue];
    if (!(state == '0004' || state == '4000')) continue;
    NSString *awayString = [NSString stringWithFormat:@"/away %@", newValue];
    NSAppleEventDescriptor *setAwayMessageEvent = [AEVT class:kCoreEventClass
                                                           id:'type'
                                               targetBundleID:[self appBundleID],
                                                   [KEY : keyDirectObject], [STRING : awayString],
                                                   [KEY : 'tyin'],
                                                   [RECORD : cObjectSpecifier,
                                                    [KEY : keyAEKeyForm], [ENUM : formAbsolutePosition],
                                                    [KEY : keyAEDesiredClass], [TYPE : 'CONN'],
                                                    [KEY : keyAEKeyData], [INT : i],
                                                    [KEY : keyAEContainer], [DESC null],
                                                    ENDRECORD],
                                                   ENDRECORD];
    require_ae_noerr(setAwayMessageEvent, cantHandleAppleEvent);
    reply = [setAwayMessageEvent sendWithImmediateReply];
    require_ae_noerr(reply, cantHandleAppleEvent);
  }
  StatzUpdateValue value = kStatzUpdateNotChanged;
  if (count != 0) {
    [oldValue_ autorelease];
    oldValue_ = [newValue copy];
    value = kStatzUpdateSuccess;
  }
  return value;
cantHandleAppleEvent:
  return kStatzUpdateError;
}

- (unsigned int)stateMenuMask { 
  return kStatzStateAvailable 
  | kStatzStateAway;
}
@end

